extends CharacterBody2D

@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var sprite_2d: Sprite2D = $Sprite2D
@onready var timer: Timer = $Timer

@export var SPEED := 50.0
@export var idle_timer := 4.0
@export var walk_timer := 2.0
var direction : Vector2 = Vector2.ZERO

func _ready() -> void:
	pick_new_state()

func get_direction():
	direction = Vector2(randi_range(-1, 1), randi_range(-1, 1))
	if(direction.x < 0):
		sprite_2d.flip_h = true
	else: 
		sprite_2d.flip_h = false
	
func pick_new_state() -> void:
	print(animation_player.current_animation )
	if animation_player.current_animation != 'walk_right':
		animation_player.play("walk_right") 
		get_direction()
		velocity = direction.normalized() * SPEED
		timer.start(walk_timer)
	else:
		animation_player.play("idle_right")
		velocity = Vector2.ZERO
		timer.start(idle_timer)
	
func _physics_process(delta: float) -> void:
	move_and_slide()


func _on_timer_timeout() -> void:
	pick_new_state()
